package com.shawbs.vueblog.controller;


import com.shawbs.vueblog.comom.PagePrams;
import com.shawbs.vueblog.comom.Result;
import com.shawbs.vueblog.entity.Blog;
import com.shawbs.vueblog.service.BlogService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.Collection;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 关注公众号：MarkerHub
 * @since 2021-08-12
 */
@Api(tags = "博客模块")
@RestController
@RequestMapping("/blog")
public class BlogController {
    @Autowired
    private BlogService blogService;

    @ApiOperation(value = "获取博客列表",notes ="json串参数：page:页码，pageSize:条数，keyword:关键字")
    @PostMapping("/listPage")
    public Result listPage(@RequestBody Map<String,Object> map
    ){
        int page = (int) map.get("page");
        int pageSize = (int) map.get("pageSize");
        String keyword = (String) map.get("keyword");
        return blogService.listPage(page, pageSize,keyword);
    }

    @ApiOperation(value = "根据ID获取博客信息")
    @GetMapping("/{id}")
    public Result getById(@PathVariable("id") Long id){
        return blogService.getById(id);
    }

    @ApiOperation(value = "根据ID获取博客信息")
    @GetMapping("/getById")
    public Result getBlog(@RequestParam(value = "id") Long id){
        return blogService.getById(id);
    }

    @RequiresAuthentication
    @ApiOperation(value = "新增博客")
    @PostMapping("/add")
    public Result add(@Validated @RequestBody Blog blog){
        return blogService.addBlog(blog);
    }

    @RequiresAuthentication
    @ApiOperation(value = "编辑博客")
    @PostMapping("/edit")
    public Result edit(@Validated @RequestBody Blog blog){
        return blogService.editBlog(blog);
    }

    @RequiresAuthentication
    @ApiOperation(value = "批量删除博客")
    @PostMapping("/deletes")
    public Result deletes(@RequestBody Collection<Long> ids){
        return blogService.removeBlogs(ids);
    }

    @RequiresAuthentication
    @ApiOperation(value = "删除博客")
    @PostMapping("/delete")
    public Result deletes(@RequestParam(value = "id") Long id){
        return blogService.removeBlog(id);
    }
}
